﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Studio.Network.Utilities;
using System.IO;
using Studio.Foundation.ImageBox;

namespace Studio.Windows.CCBVerifyCode
{
    public partial class FormMain : Form
    {
        private string urlProductTouch =
            "http://ccb.99wuxian.com/wap/queryscProductDetailccb.do?id=E9912041210&push=true&appCode=ccbSmart&type=040013";

        private string urlLoginPage = "http://ccb.99wuxian.com/wap/myaccount/loginPage.do";
        
        private string urlLoginPost = "http://ccb.99wuxian.com/wap/myaccount/redirectpage.do";

        private string urlLoginCaptcha = "http://ccb.99wuxian.com/wap/jcaptcha?time=";

        private string urlVerifyCode = "http://ccb.99wuxian.com/wap/seckilljcaptcha";

        string userAgent = "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5";

        private HttpHelper http;

        public FormMain()
        {
            InitializeComponent();
            http = new HttpHelper();
            http.UserAgent = userAgent;
        }


        private void buttonLogin_Click(object sender, EventArgs e)
        {
            string url = string.Format("{0}?userName={1}&password={2}&jcaptcha={3}&log=登录&page=", urlLoginPost,
                                       textBoxUsername.Text, textBoxPassword.Text, textBoxCaptcha.Text);
            string response = http.POST(url);
            this.Out.OutputLine(response);

        }

        private void pictureBoxLogin_Click(object sender, EventArgs e)
        {
            pictureBoxLogin.Image = http.GetImage(urlLoginCaptcha);
        }

        private void pictureBoxVerifyCode_Click(object sender, EventArgs e)
        {
            pictureBoxVerifyCode.Image = this.readCode();
        }

        Image readCode()
        {
            return http.GetImage(urlVerifyCode);
        }

        private void buttonProduct_Click(object sender, EventArgs e)
        {
            this.checkProduct();
        }

        bool checkProduct()
        {
            string response = http.GET(urlProductTouch);
            bool r = response.Contains("touch");
            if(false == r)
                this.Out.OutputLine(response);
            return r;
        }


        private string dir = "Codes";

        private int count = 0;

        private void buttonSave_Click(object sender, EventArgs e)
        {
            timerSave.Interval = trackBar1.Value;
            timerSave.Enabled = !timerSave.Enabled;

            if (false == Directory.Exists(dir))
                Directory.CreateDirectory(dir);

            int max = 10000;
            for (count = 0; count < max; count++)
            {
                string name = string.Format("{0}\\{1}-0.jpg", dir, count);
                if (false == File.Exists(name))
                    break;

            }

            if(count >= max)
                this.Out.OutputLine("finished!");
            else
            {
                this.Out.OutputLine("start at " + count.ToString() +"!");
                timerSave.Enabled = true;
            }
        }

        private void timerSave_Tick(object sender, EventArgs e)
        {
            bool login = this.checkProduct();
            if(login == false)
            {
                this.timerSave.Enabled = false;
                this.Out.OutputLine("Need Re-Login!");
                return;
            }


            for (int n = 0; n < 10; n++)
            {
                string name = string.Format("{2}\\{0}-{1}.jpg", count, n, dir);
                Image image = this.readCode();
                if(null == image || image.Height != 40)
                {
                    this.Out.OutputLine("image error.");
                    this.timerSave.Enabled = false;
                    return;
                }
                this.pictureBoxVerifyCode.Image = image;
                this.Out.OutputLine("saving " + name + "... width:{0},height:{1}", image.Width, image.Height);
                Application.DoEvents();
                image.Save(name);
            }
            count++;
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            this.timerSave.Interval = trackBar1.Value;
        }

        private void buttonGenerate_Click(object sender, EventArgs e)
        {
            string s = textBoxVerifyCode.Text;
            using (Font f = new Font("宋体", 40f))
            {
                Image image = Captcha.CreateImage(s, new Size(64, 64), f, Color.LightSalmon);
                this.pictureBoxCode1.Image = image;
            }
        }
    }
}
